package com.coin900.coin.base.utils;

import com.value.mybatis.Pagination;
import com.value.utils.RegExpUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * Unibon
 * <p/>
 * Copyright (c) 2012 YouPeng ValueSoft Inc., All rights reserved.
 * <p/>
 * This software is the confidential and proprietary information of
 * ValueSoft Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with ValueSoft.com.
 * <p/>
 * Revision History
 * Date      		Programmer       Notes
 * 13-9-4   	     laborc		     Initial
 * *********************************************************************
 */
public class RequestUtil {
    public static Pagination buildPage(HttpServletRequest request, ModelAndView mav) {
        Pagination page = new Pagination();

        String pageSizeStr = request.getParameter("pageSize");
        if (StringUtils.isBlank(pageSizeStr)) {
            page.setPageSize(10);
        } else {
            if (RegExpUtil.isNumeric(pageSizeStr)) {
                Integer pageSize = Integer.valueOf(pageSizeStr);
                page.setPageSize(pageSize == 0 ? 1 : pageSize);
            }
        }

        String currentPageStr = request.getParameter("currentPage");
        if (StringUtils.isBlank(currentPageStr)) {
            page.setCurrentPage(1);
        } else {
            if (RegExpUtil.isNumeric(currentPageStr)) {
                Integer currentPage = Integer.valueOf(currentPageStr);
                page.setCurrentPage(currentPage == 0 ? 1 : currentPage);
            }
        }
        mav.addObject("pagination", page);
        return page;
    }

    public static Pagination buildPage(HttpServletRequest request) {
        Pagination page = new Pagination();

        String pageSizeStr = request.getParameter("pageSize");
        if (StringUtils.isBlank(pageSizeStr)) {
            page.setPageSize(10);
        } else {
            if (RegExpUtil.isNumeric(pageSizeStr)) {
                Integer pageSize = Integer.valueOf(pageSizeStr);
                page.setPageSize(pageSize == 0 ? 1 : pageSize);
            }
        }

        String currentPageStr = request.getParameter("currentPage");
        if (StringUtils.isBlank(currentPageStr)) {
            page.setCurrentPage(1);
        } else {
            if (RegExpUtil.isNumeric(currentPageStr)) {
                Integer currentPage = Integer.valueOf(currentPageStr);
                page.setCurrentPage(currentPage == 0 ? 1 : currentPage);
            }
        }

        page.setStart((page.getCurrentPage() - 1) * page.getPageSize());
        return page;
    }

    public static String getIp(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        if (ip != null) {
            String[] ipSplit = StringUtils.split(ip, ",");
            if (ipSplit.length > 1) {
                ip = ipSplit[0];
            }
        }
        return ip;
    }
}
